package com.phoenix.util;

import com.phoenix.entity.GroupChampionship;
import com.phoenix.entity.MemberChampionship;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class ExcelUtil {
    public void exportForMember(HttpServletResponse response, MemberChampionship memberChampionship) throws IOException {
        // Excel2003版本（包含2003）以前使用HSSFWorkbook类，扩展名为.xls
        // Excel2007版本（包含2007）以后使用XSSFWorkbook类，扩展名为.xlsx

        // 创建工作簿类
        XSSFWorkbook workbook = new XSSFWorkbook();
        try (workbook) {
// 创建工作表并设置表名
            XSSFSheet sheet = workbook.createSheet(memberChampionship.getSinglePlayerName() + "单项赛");

            // 创建行，下标从0开始
            XSSFRow row = sheet.createRow(0);

            // 在行中创建列并赋值，下标从0开始
            row.createCell(0).setCellValue("单项赛");
            row = sheet.createRow(1);
            row.createCell(0).setCellValue("单打");
            row = sheet.createRow(2);
            row.createCell(1).setCellValue("姓名");
            row.createCell(2).setCellValue("身份证");
            row.createCell(3).setCellValue("性别");
            row.createCell(4).setCellValue("年龄");
            row.createCell(5).setCellValue("性质");
            row = sheet.createRow(3);
            row.createCell(1).setCellValue(memberChampionship.getSinglePlayerName());
            row.createCell(2).setCellValue(memberChampionship.getSinglePlayerIDCard());
            row.createCell(3).setCellValue(memberChampionship.getSinglePlayerSex());
            row.createCell(4).setCellValue(memberChampionship.getSinglePlayerAge());
            row.createCell(5).setCellValue(memberChampionship.getSinglePlayerRole());

            row = sheet.createRow(4);
            row.createCell(0).setCellValue("双打");
            row = sheet.createRow(5);
            row.createCell(1).setCellValue("姓名");
            row.createCell(2).setCellValue("身份证");
            row.createCell(3).setCellValue("性别");
            row.createCell(4).setCellValue("年龄");
            row.createCell(5).setCellValue("性质");
            row = sheet.createRow(3);
            row.createCell(1).setCellValue(memberChampionship.getGroupPlayerName());
            row.createCell(2).setCellValue(memberChampionship.getGroupPlayerIDCard());
            row.createCell(3).setCellValue(memberChampionship.getGroupPlayerSex());
            row.createCell(4).setCellValue(memberChampionship.getGroupPlayerAge());
            row.createCell(5).setCellValue(memberChampionship.getGroupPlayerRole());

            row = sheet.createRow(4);
            row.createCell(1).setCellValue(memberChampionship.getGroupPlayerName2());
            row.createCell(2).setCellValue(memberChampionship.getGroupPlayerIDCard2());
            row.createCell(3).setCellValue(memberChampionship.getGroupPlayerSex2());
            row.createCell(4).setCellValue(memberChampionship.getGroupPlayerAge2());
            row.createCell(5).setCellValue(memberChampionship.getGroupPlayerRole2());

            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/octet-stream"); // 改成输出excel文件
            response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(memberChampionship.getSinglePlayerName() + "单项赛.xlsx", "UTF-8"));
            try {
                ServletOutputStream out = response.getOutputStream();
                workbook.write(out);
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void exportForGroup(HttpServletResponse response, GroupChampionship groupChampionship) throws IOException {
        // Excel2003版本（包含2003）以前使用HSSFWorkbook类，扩展名为.xls
        // Excel2007版本（包含2007）以后使用XSSFWorkbook类，扩展名为.xlsx

        // 创建工作簿类
        XSSFWorkbook workbook = new XSSFWorkbook();
        try (workbook) {
// 创建工作表并设置表名
            XSSFSheet sheet = workbook.createSheet(groupChampionship.getGroupName() + "团队赛");

            // 创建行，下标从0开始
            XSSFRow row = sheet.createRow(0);

            // 在行中创建列并赋值，下标从0开始
            row.createCell(0).setCellValue("团队赛");
            row = sheet.createRow(1);
            row.createCell(0).setCellValue("队名");
            row.createCell(1).setCellValue(groupChampionship.getGroupName());
            row = sheet.createRow(2);
            row.createCell(0).setCellValue("领队");
            row.createCell(1).setCellValue(groupChampionship.getTeamLeaderName());
            row.createCell(2).setCellValue("电话");
            row.createCell(1).setCellValue(groupChampionship.getTeamLeaderPhone());
            row = sheet.createRow(3);
            row.createCell(0).setCellValue("教练");
            row.createCell(1).setCellValue(groupChampionship.getCoachName());
            row.createCell(2).setCellValue("电话");
            row.createCell(3).setCellValue(groupChampionship.getCoachPhone());
            row = sheet.createRow(4);
            row.createCell(0).setCellValue("运动员");
            row = sheet.createRow(5);
            row.createCell(1).setCellValue("姓名");
            row.createCell(2).setCellValue("性别");
            row.createCell(3).setCellValue("年龄");
            row.createCell(4).setCellValue("身份证");
            row = sheet.createRow(6);
            row.createCell(1).setCellValue(groupChampionship.getAthleteName());
            row.createCell(2).setCellValue(groupChampionship.getAthleteSex());
            row.createCell(3).setCellValue(groupChampionship.getAthleteAge());
            row.createCell(4).setCellValue(groupChampionship.getAthleteIDCard());
            row = sheet.createRow(7);
            row.createCell(1).setCellValue(groupChampionship.getAthleteName2());
            row.createCell(2).setCellValue(groupChampionship.getAthleteSex2());
            row.createCell(3).setCellValue(groupChampionship.getAthleteAge2());
            row.createCell(4).setCellValue(groupChampionship.getAthleteIDCard2());
            row = sheet.createRow(8);
            row.createCell(1).setCellValue(groupChampionship.getAthleteName3());
            row.createCell(2).setCellValue(groupChampionship.getAthleteSex3());
            row.createCell(3).setCellValue(groupChampionship.getAthleteAge3());
            row.createCell(4).setCellValue(groupChampionship.getAthleteIDCard3());
            row = sheet.createRow(9);
            row.createCell(1).setCellValue(groupChampionship.getAthleteName4());
            row.createCell(2).setCellValue(groupChampionship.getAthleteSex4());
            row.createCell(3).setCellValue(groupChampionship.getAthleteAge4());
            row.createCell(4).setCellValue(groupChampionship.getAthleteIDCard4());
            row = sheet.createRow(10);
            row.createCell(1).setCellValue(groupChampionship.getAthleteName5());
            row.createCell(2).setCellValue(groupChampionship.getAthleteSex5());
            row.createCell(3).setCellValue(groupChampionship.getAthleteAge5());
            row.createCell(4).setCellValue(groupChampionship.getAthleteIDCard5());
            row = sheet.createRow(11);
            row.createCell(1).setCellValue(groupChampionship.getAthleteName6());
            row.createCell(2).setCellValue(groupChampionship.getAthleteSex6());
            row.createCell(3).setCellValue(groupChampionship.getAthleteAge6());
            row.createCell(4).setCellValue(groupChampionship.getAthleteIDCard6());
            row = sheet.createRow(12);
            row.createCell(1).setCellValue(groupChampionship.getAthleteName7());
            row.createCell(2).setCellValue(groupChampionship.getAthleteSex7());
            row.createCell(3).setCellValue(groupChampionship.getAthleteAge7());
            row.createCell(4).setCellValue(groupChampionship.getAthleteIDCard7());
            row = sheet.createRow(13);
            row.createCell(1).setCellValue(groupChampionship.getAthleteName8());
            row.createCell(2).setCellValue(groupChampionship.getAthleteSex8());
            row.createCell(3).setCellValue(groupChampionship.getAthleteAge8());
            row.createCell(4).setCellValue(groupChampionship.getAthleteIDCard8());
            row = sheet.createRow(14);
            row.createCell(1).setCellValue(groupChampionship.getAthleteName9());
            row.createCell(2).setCellValue(groupChampionship.getAthleteSex9());
            row.createCell(3).setCellValue(groupChampionship.getAthleteAge9());
            row.createCell(4).setCellValue(groupChampionship.getAthleteIDCard9());
            row = sheet.createRow(15);
            row.createCell(1).setCellValue(groupChampionship.getAthleteName10());
            row.createCell(2).setCellValue(groupChampionship.getAthleteSex10());
            row.createCell(3).setCellValue(groupChampionship.getAthleteAge10());
            row.createCell(4).setCellValue(groupChampionship.getAthleteIDCard10());

            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/octet-stream"); // 改成输出excel文件
            response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(groupChampionship.getGroupName() + "团体赛.xlsx", "UTF-8"));
            try {
                ServletOutputStream out = response.getOutputStream();
                workbook.write(out);
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
